package com.gxa.controller.system;

import com.gxa.annotation.Perm;
import com.gxa.pojo.entity.system.Material;
import com.gxa.pojo.entity.system.User;
import com.gxa.service.system.MaterialService;
import com.gxa.service.system.UserService;
import com.gxa.utils.MessageCode;
import com.gxa.utils.QueryResult;
import com.gxa.utils.Result;
import com.gxa.utils.ResultUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author lch
 * @Date 2024/2/22 16:04
 * @Description
 **/
@RestController
public class MaterialController {

    @Resource
    private MaterialService materialService;

    /**
     * 添加物资
     *
     * @param material
     * @return
     */
    @Perm("material:add")
    @PostMapping("/material/add")
    public Result addMaterial(Material material) {
        int result = materialService.addMaterial(material);
        if (result > 0) {
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        }
        return ResultUtils.buildFail(MessageCode.SYSTEM_ERROR);
    }

    /**
     * 更新物资
     *
     * @param material
     * @return
     */
    @Perm("material:update")
    @PostMapping("/material/update")
    public Result updateMaterial(Material material) {
        int result = materialService.updateMaterial(material);
        if (result > 0) {
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        }
        return ResultUtils.buildFail(MessageCode.SYSTEM_ERROR);
    }

    /**
     * 删除物资
     *
     * @param id 物资id
     * @return
     */
    @Perm("material:delete")
    @PostMapping("/material/delete")
    public Result deleteMaterial(Integer id) {
        int result = materialService.deleteMaterialById(id);
        if (result > 0) {
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        }
        return ResultUtils.buildFail(MessageCode.SYSTEM_ERROR);
    }


    /**
     * 查询物资
     *
     * @param material
     * @return
     */
    @Perm("material:query")
    @GetMapping("/material/query")
    public Result queryMaterial(Material material) {
        QueryResult result = materialService.queryMaterial(material);
        return ResultUtils.buildSuccess(result.getData(), result.getCount());
    }
}
